<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
</head>
<body>
<script type="text/javascript">
    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    let id=12;//getRandomInt(1,100);
    let websocket = new WebSocket("ws://localhost:9090/channel/echo/"+id+"?token=xxx");

    // 连接断开
    websocket.onclose = e => {
        console.log(`连接关闭: code=${e.code}, reason=${e.reason}`)
    }
    // 收到消息
    websocket.onmessage = e => {
        console.log(`收到消息：${e.data}`);
    }

    // 异常
    websocket.onerror = e => {
        console.log("连接异常")
        console.error(e)
    }
    // 连接打开
    websocket.onopen = e => {
        console.log("连接打开:"+id);

        // 创建连接后，往服务器连续写入3条消息
        websocket.send("springdoc.cn");
        websocket.send("springdoc.cn");
        websocket.send("springdoc.cn");

        // 最后发送 bye，由服务器断开连接
        // websocket.send("bye");

        // 也可以由客户端主动断开
        // websocket.close();
    }
</script>
</body>
</html>